/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bsc.war.form;

import bsc.ejb.metadata.Framework;
import bsc.ejb.metadata.SMTP;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class AddSMTP extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        ModelAndView mav = new ModelAndView("addSMTP");

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            SMTP smtp;
            Framework framework;

            if ((session.getAttribute("frameworkId") instanceof Long)) {
                Long frameworkId = (Long) session.getAttribute("frameworkId");
                framework = delegate.findFramework(frameworkId);
            } else {
                //Redirigir a la página principal
                throw new Exception("No existe el framework");
            }

            String type = request.getParameter("type");

            if ("edit".equals(type) && NumberUtils.isDigits(request.getParameter("id"))) {
                    mav.addObject("smtp", delegate.findSMTP(Long.valueOf(request.getParameter("id"))));
            } else {
                mav.addObject("notSmtp", delegate.findSMTP(framework, Boolean.FALSE));
            }
        } catch (Exception e) {
        }

        return mav;

    }

}
